package concurrent.masterandworker;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;

/**
 * @description: 任务类
 * @author: kanggw
 * @date: 2023/7/3
 **/
@Data
@AllArgsConstructor
@Builder
@Accessors(chain = true)
public class Task<R> {

    private static AtomicInteger index = new AtomicInteger(1);
    private int taskId;
    private int workerId;

    /**
     * 任务的回调函数
     */
    public Consumer<Task<R>> resultAction;

    R result;

    public Task() {
        this.taskId = index.getAndIncrement();
    }

    public void execute() {
        this.result = this.doExecute();
        resultAction.accept(this);
    }

    protected R doExecute() {
        //由子类实现
        return null;
    }



}
